package utd.cs6322.cluster.visualization;

import java.awt.event.MouseEvent;

import javax.swing.ToolTipManager;

import prefuse.Display;
import prefuse.controls.ControlAdapter;
import prefuse.visual.VisualItem;

/**
 * MAD Demo control which shows one way of creating a more extended tool tip.
 * Kept generic and meant as a base for individual extensions
 * 
 * @author <a href="http://goosebumps4all.net/3all">martin dudek</a>
 * 
 */
public class ToolTipControl extends ControlAdapter {

	private String[] fields;
	private String[] texts;
	private StringBuffer sbuf;

	int dismissDelay = Integer.MAX_VALUE;

	public ToolTipControl(String[] texts, String[] fields) {
		this.texts = texts;
		this.fields = fields;
		sbuf = new StringBuffer();

		ToolTipManager.sharedInstance().setInitialDelay(276);

	}

	public void itemEntered(VisualItem vi, MouseEvent e) {

		String value;

		ToolTipManager.sharedInstance().setDismissDelay(dismissDelay);

		sbuf.delete(0, sbuf.length());

		sbuf.append("<html><table>");

		boolean someInfo = false;

		for (int i = 0; i < fields.length; i++) {
			if (vi.canGetString(fields[i])) {
				value = vi.getString(fields[i]);
				if (value != null && value.length() > 0) {
					sbuf.append("<tr valign='top'><td><b>");
					sbuf.append(texts[i] + "</b></td><td>");
					sbuf.append(value);
					someInfo = true;
				}
				sbuf.append("</td></tr>");
			}
		}

		if (someInfo) {
			sbuf.append("</table>");
			sbuf
					.append("<hr size=1 width=\"97%\"><div align=\"right\"><font color=\"#999999\">esc to close&nbsp;</font></div>");
			sbuf.append("</html>");

			Display disp = (Display) e.getSource(); // the display on which
			// the event occured
			disp.setToolTipText(sbuf.toString());
		}

	}

	public void itemExited(VisualItem item, MouseEvent e) {
		Display disp = (Display) e.getSource();
		disp.setToolTipText(null);
	}

} // end of class DemoControlToolTip